/**
 * This code is originally written by Lars Vogel
 * http://www.vogella.de/articles/AndroidListView/article.html
 * and modified by me.
 *
 * @author prebz
 */

package com.wirelesstransfer.activity.filebrowser;

import java.util.List;

import com.wirelesstransfer.R;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomArrayAdapter extends ArrayAdapter<String>{
	
	
	private final Activity context;
	private final List<String> allFiles;
	private final List<String> allFilesSize; 

	/**
	 * 
	 * @param context from activity which will create the adapter
	 * @param allFiles of all the files
	 * @param allFilesSize the size of all the files
	 */
	public CustomArrayAdapter(Activity context, List<String> allFiles,List<String> allFilesSize ) {
		super(context, R.layout.row, allFiles);
		this.context = context;
		this.allFiles = allFiles;
		this.allFilesSize = allFilesSize;
	}

	// saves the reference of the object in list
	static class ViewHolder {
		public ImageView imageView;
		public TextView textViewFiles;
		public TextView textViewSize;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;

		// reuse existing views for performance
		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.row, null, true);
			holder = new ViewHolder();
			holder.textViewFiles = (TextView) rowView.findViewById(R.id.rowfile_txt);
			holder.textViewSize = (TextView) rowView.findViewById(R.id.rowfilesize_txt);
			holder.imageView = (ImageView) rowView.findViewById(R.id.row_icon);
			rowView.setTag(holder);
		} else {
			holder = (ViewHolder) rowView.getTag();
		}

		holder.textViewFiles.setText(allFiles.get(position));
		holder.textViewSize.setText(allFilesSize.get(position));
		
		// Change the icons
		String s = allFiles.get(position);
		if (s.endsWith("/")) 
			holder.imageView.setImageResource(R.drawable.green_folder_drag_accept);
		else if (s.endsWith(".pdf"))
			holder.imageView.setImageResource(R.drawable.application_pdf);
		else if (s.endsWith(".bzip"))
			holder.imageView.setImageResource(R.drawable.application_x_bzip);
		else if (s.endsWith(".img"))
			holder.imageView.setImageResource(R.drawable.application_x_cd_image);
		else if (s.endsWith(".bzip"))
			holder.imageView.setImageResource(R.drawable.application_x_bzip);
		else if (s.endsWith(".avi"))
			holder.imageView.setImageResource(R.drawable.application_x_mplayer2);
		else if (s.endsWith(".png"))
			holder.imageView.setImageResource(R.drawable.image);
		else if (s.endsWith(".zip"))
			holder.imageView.setImageResource(R.drawable.zip);
		else  
			holder.imageView.setImageResource(R.drawable.kwordquiz_do);
		
		return rowView;
	}

}
